package com.kaizzi.data.jdl.service.impl;

import com.baomidou.dynamic.datasource.annotation.DS;
import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.conditions.segments.MergeSegments;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.kaizzi.data.common.utils.DateUtil;
import com.kaizzi.data.jdl.entity.JdlPageData;
import com.kaizzi.data.jdl.entity.LogHistory;
import com.kaizzi.data.jdl.helper.*;
import com.kaizzi.data.jdl.mapper.JdlDataMapper;
import com.kaizzi.data.jdl.model.ExcelModel;
import com.kaizzi.data.jdl.service.JdlDataService;
import com.kaizzi.data.jdl.service.LogService;
import com.kaizzi.data.ppt.CreateJdlPPT;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.time.LocalDateTime;
import java.util.Arrays;
import java.util.List;

/**
 * @author Kzen
 * @Description description
 * @date 2020/9/9
 */
@Service
@Slf4j
public class JdlDataServiceImpl extends ServiceImpl<JdlDataMapper, JdlPageData> implements JdlDataService {

    @Resource
    private JdlDataMapper jdlDataMapper;
    @Resource
    private ExcelHelper excelHelper;
    @Resource
    private CreateJdlPPT createJdlPPT;
    @Resource
    private LogService logService;

    @Resource
    private PageData1 pageData1;
    @Resource
    private PageData4 pageData4;
    @Resource
    private PageData7 pageData7;
    @Resource
    private PageData8 pageData8;
    @Resource
    private PageData9 pageData9;
    @Resource
    private PageData10 pageData10;
    @Resource
    private PageData11 pageData11;
    @Resource
    private PageData12 pageData12;
    @Resource
    private PageData14 pageData14;
    @Resource
    private PageData15 pageData15;
//    @Resource
//    private PageData17 pageData17;
    @Resource
    private PageData18 pageData18;
//    @Resource
//    private PageData20 pageData20;
//    @Resource
//    private PageData21 pageData21;
//    @Resource
//    private PageData22 pageData22;
    @Resource
    private PageData24 pageData24;
    @Resource
    private PageData25 pageData25;
    @Resource
    private PageData27 pageData27;
    @Resource
    private PageData29 pageData29;
    @Resource
    private PageData30 pageData30;
    @Resource
    private PageData32 pageData32;
    @Resource
    private PageData34 pageData34;
    @Resource
    private PageData35 pageData35;
    @Resource
    private PageData37 pageData37;
    @Resource
    private PageData38 pageData38;
    @Resource
    private PageData39 pageData39;
    @Resource
    private PageData41 pageData41;
    @Resource
    private PageData42 pageData42;
    @Resource
    private PageData45 pageData45;
    @Resource
    private PageData46 pageData46;

    @Override
    @DS("apdata")
    public String getPageData(String page) {
        //根据日期和page 获取上周末内容
        return getJdlPageData(page).getJdlData();
    }

    @Override
    @DS("apdata")
    public int getDataStatus() {
        String lastSunday = DateUtil.getLastSundayFormat();
        long size = jdlDataMapper.getDataCount(lastSunday);
        return (int)size;
    }

    //解析excel数据
    @Override
    public void analyticalExcelData() {
        ExcelModel model = excelHelper.readExcel();
        if (model == null){
            return;
        }
        List<String> pageList = getPageList();
        //添加状态信息
        logService.setDataLogStatus("data-init");//初始化
        try {
            logService.setDataLogStatus("data-exe");
            for (String s : pageList) {
                traversePageData(model,s);
            }
        }catch (Exception e){
            logService.setDataLogStatus("data-error");
            return;
        }
        logService.setDataLogStatus("data-error");
        //数据少于30条 直接返回
        if (jdlDataMapper.getDataCount(DateUtil.getLastSundayFormat()) != 30){
            return;
        }else {
            logService.setDataLogStatus("data-success");
        }
        //生成成功直接使用数据替换模板ppt
        logService.setDataLogStatus("ppt-init");
        createJdlPPT.createPPT("jdl.pptx","temp1118.pptx");
//        logService.setDataLogStatus("ppt-success");
    }

    //合并excel、数据库数据,
    private void traversePageData(ExcelModel model,String page) {
        String data = "";
        switch (page){
            case "1":
                data = pageData1.getData();
                break;
            case "4":
                data = pageData4.getData(model);
                break;
            case "7":
                data = pageData7.getData(model);
                break;
            case "8":
                data = pageData8.getData(model);
                break;
            case "9":
                data = pageData9.getData(model);
                break;
            case "10":
                data = pageData10.getData(model);
                break;
            case "11":
                data = pageData11.getData(model);
                break;
            case "12":
                data = pageData12.getData(model);
                break;
            case "14":
                data = pageData14.getData();
                break;
            case "15":
                data = pageData15.getData(model);
                break;
//            case "17":
//                data = pageData17.getData(model);
//                break;
            case "18":
                data = pageData18.getData(model);
                break;
//            case "20":
//                data = pageData20.getData(model);
//                break;
//            case "21":
//                data = pageData21.getData(model);
//                break;
//            case "22":
//                data = pageData22.getData(model);
//                break;
            case "24":
                data = pageData24.getData(model);
                break;
            case "25":
                data = pageData25.getData(model);
                break;
            case "27":
                data = pageData27.getData(model);
                break;
            case "29":
                data = pageData29.getData(model);
                break;
            case "30":
                data = pageData30.getData(model);
                break;
            case "32":
                data = pageData32.getData(model);
                break;
            case "34":
                data = pageData34.getData(model);
                break;
            case "35":
                data = pageData35.getData(model);
                break;
            case "37":
                data = pageData37.getData(model);
                break;
            case "38":
                data = pageData38.getData(model);
                break;
            case "39":
                data = pageData39.getData(model);
                break;
            case "41":
                data = pageData41.getData(model);
                break;
            case "42":
                data = pageData42.getData(model);
                break;
            case "45":
                data = pageData45.getData(model);
                break;
            case "46":
                data = pageData46.getData();
                break;
            default:
                break;
        }
        //验证是否存在
        JdlPageData tempData = getJdlPageData(page);
        //新建
        JdlPageData pageData = new JdlPageData();
        pageData.setJdlPage(page);
        pageData.setJdlData(data);
        pageData.setUpdateTime(DateUtil.getDateTimeNow());
        if (tempData!=null){
            pageData.setId(tempData.getId());
            jdlDataMapper.updateById(pageData);
            logService.recordLog("update",2);
            log.info("page:{},更新数据成功",page);
            return;
        }
        pageData.setJdlDate(DateUtil.getLastSundayFormat());
        pageData.setCreateTime(DateUtil.getDateTimeNow());
        jdlDataMapper.insert(pageData);
        logService.recordLog("success",2);
        log.info("page:{},新增数据成功",page);
    }

    //获取ppt页码
    private List<String> getPageList() {
        //暂时写死 后续写入数据库中
        return Arrays.asList("1","4","7","8","9","10","11","12","14","15","17","18","20","21","22","24","25","27","29","30","32","34","35","37","38","39","41","42","45","46");
    }

    @Override
    @DS("apdata")
    public void saveJdlData(){
        JdlPageData data = new JdlPageData();
        jdlDataMapper.saveJdlData(data);
    }

    @Override
    @DS("apdata")
    public JdlPageData getJdlPageData(String page){
        Wrapper<JdlPageData> wrapper = new Wrapper<JdlPageData>() {
            @Override
            public JdlPageData getEntity() {
                JdlPageData pageData1 = new JdlPageData();
                pageData1.setJdlPage(page);
                pageData1.setJdlDate(DateUtil.getLastSundayFormat());
                return pageData1;
            }
            @Override
            public MergeSegments getExpression() {return null;}
            @Override
            public String getSqlSegment() {return null;}
        };
        return jdlDataMapper.selectOne(wrapper);
    }
}
